<template>
    <!-- 对话框表单 -->
    <!-- 建议使用 Prettier 格式化代码 -->
    <!-- el-form 内可以混用 el-form-item、FormItem、ba-input 等输入组件 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="config.layout.shrink ? '':'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    :label-position="config.layout.shrink ? 'top' : 'right'"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >

<!--                    <FormItem :label="t('shop.category_ids')" type="remoteSelects" v-model="baTable.form.items!.category_ids" prop="category_ids" :input-attr="{ pk: 'xxz_category.id', field: 'name', 'remote-url': '/admin/Category/index' }" :placeholder="t('Please select field', { field: t('shop.category_ids') })" />-->
                    <el-alert v-if="useAdminInfo().admin_type != 1" type="info" show-icon :closable="false">
                        <p>商家管理账号需要输入完整手机号搜索切换绑定用户</p>
                    </el-alert>
                    <FormItem :label="t('shop.user_nickname')" type="remoteSelect" v-model="baTable.form.items!.user_id" prop="user_id" :input-attr="{ pk: 'xxz_user.id', field: 'nickname_text', 'remote-url': '/admin/user.User/index' }" :placeholder="t('Please select field', { field: t('shop.user_nickname') })" />

                    <FormItem :label="t('shop.name')" type="string" v-model="baTable.form.items!.name" prop="name" :placeholder="t('Please input field', { field: t('shop.name') })" />
                    <FormItem :label="t('shop.wx_number')" type="string" v-model="baTable.form.items!.wx_number" prop="wx_number" :placeholder="t('Please input field', { field: t('shop.wx_number') })" />

                    <FormItem :label="t('shop.phone')" type="string" v-model="baTable.form.items!.phone" prop="phone" :placeholder="t('Please input field', { field: t('shop.phone') })" />

                    <FormItem :label="t('shop.image')" type="image" v-model="baTable.form.items!.image" prop="image" />
                    <FormItem :label="t('shop.images')" type="images" v-model="baTable.form.items!.images" prop="images" />

                    <FormItem :label="t('shop.business_image')" type="image" v-model="baTable.form.items!.business_image" prop="business_image" />

<!--
                    <div id="el-id-9389-94" class="el-form-item__label" style="width: 160px;"><div data-v-a2a57f41="" class="ba-form-item-label"><div data-v-a2a57f41="">地图选点</div><div data-v-a2a57f41="" class="ba-form-item-label-tip"></div></div></div>

                    <iframe id="mapPage" width="80%" height="500px" style="margin-left: 160px" frameborder=0
                            src="https://apis.map.qq.com/tools/locpicker?search=1&type=1&key=WTMBZ-JUQLS-P6EOO-6P6G3-T2A4O-WHBNO&referer=外卖福利平台-用户定位">
                    </iframe>
-->

                    <FormItem :label="t('shop.city')" type="city" v-model="baTable.form.items!.city" prop="city" :placeholder="t('Please select field', { field: t('shop.city') })" />
                    <FormItem :label="t('shop.address')" type="string" v-model="baTable.form.items!.address" prop="address" :placeholder="t('Please input field', { field: t('shop.address') })" />


<!--                    <FormItem :label="t('shop.lng')" type="string" v-model="baTable.form.items!.lng" prop="lng" :placeholder="t('Please input field', { field: t('shop.lng') })" />-->
<!--                    <FormItem :label="t('shop.lat')" type="string" v-model="baTable.form.items!.lat" prop="lat" :placeholder="t('Please input field', { field: t('shop.lat') })" />-->

<!--                    <FormItem :label="t('shop.publicity')" type="string" v-model="baTable.form.items!.publicity" prop="publicity" :placeholder="t('Please input field', { field: t('shop.publicity') })" />-->

<!--                    <FormItem :label="t('shop.definition_name')" type="string" v-model="baTable.form.items!.definition_name" prop="definition_name" :placeholder="t('Please input field', { field: t('shop.definition_name') })" />-->
<!--                    <div class="el-form-item__content" style="margin-left: 160px;color: #4397fd;margin-top: -20px;">-->
<!--                        * 自定义名称在门店详情里面展示，功能为线下核销的产品：如游泳馆可填写‘游泳票’-->
<!--                    </div>-->

<!--                    <FormItem :label="t('shop.open_week')" type="checkbox" v-model="baTable.form.items!.open_week" prop="open_week" :data="{ content: { '0': t('shop.open_week 0'), '1': t('shop.open_week 1'), '2': t('shop.open_week 2'), '3': t('shop.open_week 3'), '4': t('shop.open_week 4'), '5': t('shop.open_week 5'), '6': t('shop.open_week 6') } }" :placeholder="t('Please select field', { field: t('shop.open_week') })" />-->

<!--                    <FormItem  :label="t('shop.open_start_time')" type="time"  v-model="baTable.form.items!.open_start_time" prop="open_start_time" :placeholder="t('Please select field', { field: t('shop.open_start_time') })"  />-->

<!--                    <FormItem :label="t('shop.open_end_time')" type="time"  v-model="baTable.form.items!.open_end_time" prop="open_end_time" :placeholder="t('Please select field', { field: t('shop.open_end_time') })"  />-->

<!--                    <FormItem :label="t('shop.open_status')" type="radio" v-model="baTable.form.items!.open_status" prop="open_status" :data="{ content: { '0': t('shop.open_status 0'), '1': t('shop.open_status 1'), '2': t('shop.open_status 2') } }" :placeholder="t('Please select field', { field: t('shop.open_status') })" />-->

                    <span v-if="useAdminInfo().admin_type == 1">
                        <label id="el-id-6970-63" for="el-id-6970-77" class="el-form-item__label" style="width: 160px;margin-bottom: 15px"><h4>平台配置：</h4></label><br>
                        <FormItem  :label="t('shop.up_goods_num')" type="number" v-model="baTable.form.items!.up_goods_num" prop="up_goods_num" :placeholder="t('Please input field', { field: t('shop.up_goods_num') })" />
                        <FormItem :label="t('shop.status')"  type="switch" v-model="baTable.form.items!.status" prop="status" :data="{ content: { '0': t('shop.status 0'), '1': t('shop.status 1') } }" />
                    </span>

                    <label id="el-id-6970-63" for="el-id-6970-77" class="el-form-item__label" style="width: 160px;margin-bottom: 15px"><h4>管理账号设置：</h4></label><br>
                    <FormItem :label="t('shop.account')" type="string" v-model="baTable.form.items!.account" prop="account" :placeholder="t('Please input field', { field: t('shop.account') })" />
                    <FormItem :label="t('shop.password')" type="password" v-model="baTable.form.items!.password" placeholder="首次添加账号必填密码。不修改密码可为空" />

                    <label id="el-id-6970-63" for="el-id-6970-77" class="el-form-item__label" style="width: 160px;margin-bottom: 15px"><h4>退换货邮寄设置：</h4></label><br>
                    <FormItem :label="t('shop.mail_name')" type="string" v-model="baTable.form.items!.mail_name" prop="mail_name" :placeholder="t('Please input field', { field: t('shop.mail_name') })" />
                    <FormItem :label="t('shop.mail_mobile')" type="string" v-model="baTable.form.items!.mail_mobile" prop="mail_mobile" :placeholder="t('Please input field', { field: t('shop.mail_mobile') })" />
                    <FormItem :label="t('shop.mail_address')" type="string" v-model="baTable.form.items!.mail_address" prop="mail_address" :placeholder="t('Please input field', { field: t('shop.mail_address') })" />


                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm()">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import type { FormInstance, FormItemRule } from 'element-plus'
import { inject, reactive, ref } from 'vue'
import { useI18n } from 'vue-i18n'
import FormItem from '/@/components/formItem/index.vue'
import { useConfig } from '/@/stores/config'
import type baTableClass from '/@/utils/baTable'
import { buildValidatorData } from '/@/utils/validate'
import createAxios from "/@/utils/axios";
import {useAdminInfo} from "/@/stores/adminInfo";

const config = useConfig()
const formRef = ref<FormInstance>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    category_ids: [buildValidatorData({ name: 'required', title: t('shop.category_ids') })],
    name: [buildValidatorData({ name: 'required', title: t('shop.name') })],
    phone: [buildValidatorData({ name: 'required', title: t('shop.phone') })],
    image: [buildValidatorData({ name: 'required', title: t('shop.image') })],
    business_image: [buildValidatorData({ name: 'required', title: t('shop.business_image') })],
    images: [buildValidatorData({ name: 'required', title: t('shop.images') })],
    city: [buildValidatorData({ name: 'required', title: t('shop.city') })],
    address: [buildValidatorData({ name: 'required', title: t('shop.address') })],
    lng: [buildValidatorData({ name: 'required', title: t('shop.lng') })],
    lat: [buildValidatorData({ name: 'required', title: t('shop.lat') })],
    definition_name: [buildValidatorData({ name: 'required', title: t('shop.definition_name') })],
    open_week: [buildValidatorData({ name: 'required', title: t('shop.open_week') })],
    open_start_time: [buildValidatorData({ name: 'required', title: t('shop.open_start_time') })],
    open_end_time: [buildValidatorData({ name: 'required', title: t('shop.open_end_time') })],
    open_status: [buildValidatorData({ name: 'required', title: t('shop.open_status') })],
    status: [buildValidatorData({ name: 'required', title: t('shop.status') })],
    create_time: [buildValidatorData({ name: 'date', title: t('shop.create_time') })],
    update_time: [buildValidatorData({ name: 'date', title: t('shop.update_time') })],
})


window.addEventListener('message', function(event) {
    // 接收位置信息，用户选择确认位置点后选点组件会触发该事件，回传用户的位置信息
    var loc = event.data;
    if (loc && loc.module == 'locationPicker') {//防止其他应用也会向该页面post信息，需判断module是否为'locationPicker'
        console.log('location', loc);
        baTable.form.items.lng = loc.latlng.lng
        baTable.form.items.lat = loc.latlng.lat


        createAxios({
            url: '/admin/ajax/analysis_address?address='+loc.poiaddress,
            method: 'get',
            data: {

            }
        }, {
            loading: false
        }, {
            text: '请求中...'
        })
            .then((res) => {
                baTable.form.items.city = ref([
                    res.data.array.province_id,
                    res.data.array.city_id,
                    res.data.array.area_id,
                ]);
                baTable.form.items.address = res.data.array.address+loc.poiname
            })
            .catch((resx) => {
            })


    }
}, false);
</script>

<style scoped lang="scss"></style>
